Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
target "this" on addEventListener event

So I'm dealing with a little problem..

I'm trying to modify drag-and-drop code and came across a problem, since I want to have multiple drop areas and one function for all of them.

I have this code:

dropArea.addEventListener("dragover", (event)=>{
  event.preventDefault(); //preventing from default behaviour
  dropArea.classList.add("activated");
  dragText.textContent = "🖐 Release to Upload File";
});

And specified dropareawitth querySelector:

const dropArea = document.querySelector(".drag-area")

Now I need to modify it to work with multiple drop zones. I thought to just replace the "droparea" inside the event to "this" something like that:

this.classList.add("activated");

but it does not work. I even tried:

$(this).classList.add("activated");

Returns this error:

Uncaught TypeError: Cannot read properties of undefined (reading 'add')

Any solutions? I'm stuck...

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As your event handler is an arrow function, the value of this is not influenced by the caller, so you cannot use it to get the DOM element. You can use event.target.

event.target.classList.add("activated");

As to your final attempt: $() does not return a DOM element, but an array-like "jQuery" collection object, which doesn't have such properties as classList.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda